<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@include file="/common/session.jsp" %>
<c:set var="contractId" value="${param.contractId}"/>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <LINK type="text/css" rel="stylesheet" href="${ctx}/css/default.css"/>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/ContractRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/MarketRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/ShopRpcService.js'></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body>
<form id="contractForm" name="contractForm">
	<center>
        <input type="hidden" id="contractId" name="contractId" value="${contractId}">
        <table width="100%" cellpadding="5" cellspacing="5">
            <tr>
                <td>
                    签订市场:
                </td>
                <td>
                    <select id="marketId" name="marketId" onchange="getShopList()">
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    签订商户:
                </td>
                <td>
                    <select id="shopId" name="shopId">
			        </select>
                </td>
                <td>
                   合同状态:
                </td>
                <td>
                    <select id="contractState" name="contractState">
			            <option value="1">有效</option>
			            <option value="0">无效</option>
			        </select>
                </td>
            </tr>
            <tr>
            	<td>
            	合同有效期:
            	</td>
            </tr>
            <tr>
            	<td>
            	 	开始时间:
            	</td>
                <td>
                	
                    <input type="date" id="contractStartDate"  name="contractStartDate" />
                </td>
                <td>
            	 	截止时间:
            	</td>
                <td>
                    <input type="date" id="contractEndingDate" name="contractEndingDate" />
                </td>
            </tr>
            <tr>
                <td style="border-bottom: 1px solid #e1ecf5" colspan="6">
                </td>
            </tr>
            <tr>
                <td colspan="6" align="center">
                    <button type="button" onclick="insertOrUpdateContract()">保存</button>
                    <button type="button" onclick="callbackContract()">返回</button>
                </td>
            </tr>
        </table>
    </center>
</form>
</body>
</html>

<script>

    //    显示市场列表
    var market = new Market();
    market.marketId = "${marketId}";
    MarketRpcService.selectMarketList(market, callbackSelectMarketList);

    function callbackSelectMarketList(list) {
        dwr.util.addOptions("marketId", list, "marketId", "marketName");
        dwr.util.setValue("marketId", "${marketId}");
        getShopList();
        if ("${contractId}".length > 0) {
            selectContractList();
        }
        else{
            setTime();
        }
    }

    function getShopList(){
        var shop = new Shop();
        shop.marketId = dwr.util.getValue("marketId");
        shop.shopState = "1";
        ShopRpcService.selectShopList(shop, callbackSelectShopList);
    }

    function callbackSelectShopList(list) {
        dwr.util.removeAllOptions("shopId");
        dwr.util.addOptions("shopId", {"" : "请选择"});
        dwr.util.addOptions("shopId", list, "shopId", "shopName");
    }


    //    修改显示页面内容
    function selectContractList() {
        var contract = new Contract();
        contract.contractId = "${contractId}";
        contract.marketId = "${marketId}";
        ContractRpcService.selectContractList(contract, callbackSelectContractList);
    }

    function callbackSelectContractList(list) {
        dwr.util.setValues(list[0]);
        dwr.util.setValue("contractStartDate", list[0].contractStartDate.Format("yyyy-MM-dd"));
        dwr.util.setValue("contractEndingDate", list[0].contractEndingDate.Format("yyyy-MM-dd"));
    }


    //    新增设置默认时间
    function setTime(){
        var startTime = new Date();
        var year = startTime.getFullYear() + 1;
        dwr.util.setValue("contractStartDate", startTime.Format("yyyy-MM-dd"));
        dwr.util.setValue("contractEndingDate", year+startTime.Format("-MM-dd"));
    }


    //    新增合同
    function insertOrUpdateContract() {
        if (!validate($("contractForm"))) {
            return false
        }
        if (dwr.util.getValue("shopId") == ''){
            alert('请选择商铺')
            return false
        }
        var contract = dwr.util.getValues("contractForm");
        var contractStartDate = dwr.util.getValue("contractStartDate");
        var contractEndingDate = dwr.util.getValue("contractEndingDate");
        contract.contractStartDate = new Date(contractStartDate);
        contract.contractEndingDate = new Date(contractEndingDate);
        if ("${contractId}".length > 0) {
            ContractRpcService.updateContract(contract, callbackContractRegion("${contractId}"));
        }else{
            ContractRpcService.insertContract(contract, callbackContractRegion);
        }
    }

    function callbackContractRegion(contract){
        if ("${contractId}".length > 0){
            location = "${ctx}/contract/contract_region_list.jsp?contractId=${contractId}";
        }
        else {
            location = "${ctx}/contract/contract_region_list.jsp?contractId=" + contract.contractId;

        }
    }

    function callbackContract(){
        <%--location = "${ctx}/contract/contract_list.jsp";--%>
        window.history.back();
    }

</script>
